import { login, logout, getInfo } from '@/api/login'
import { getToken, setToken, removeToken } from '@/utils/auth'

const user = {
  state: {
    token: getToken(),
    name: '',
    avatar: '',
    roles: [],
    msg:0
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_NAME: (state, name) => {
      state.name = name
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    },
    SET_TENANTNAME: (state, tenantname) => {
      state.tenantname = tenantname
    },
    SET_MSG: (state, msg) => {
      state.msg = msg
    },
  },

  actions: {
    maknewtoken({commit},data){
      return new Promise((resolve, reject) => {
        setToken(data)
        commit('SET_TOKEN', data);
        resolve('1111')
      })
      
     
    },
    //消息状态改变
    msgstate({commit,state},data){
      let num = state.msg + 1
      console.log(state.msg)
      console.log(state)
      console.log(commit)
      console.log(data)
      commit('SET_MSG', num);
      console.log(state.msg)
    },
    // 登录
    Login({ commit }, datas) {
      // const username = userInfo.username.trim()
      return new Promise((resolve, reject) => {
        login(datas.userinfo).then(response => {
          if (response.success) {
            const data = response.result;
            if (datas.remember) {
              setToken(data, 5)
            } else {
              setToken(data)
            }

            commit('SET_TOKEN', data);
            resolve(response)
          } else {
            resolve(response)
          }

        }).catch(error => {

          reject(error)
        })
      })
    },

    // 获取用户信息
    GetInfo({ commit, state }) {
      console.log(getToken())
      return new Promise((resolve, reject) => {
        getInfo({}).then(response => {
          if(response.success){
            let data = response.result;
            localStorage.setItem('baseuserinfo', JSON.stringify(data));
            commit('SET_NAME', data)
  
            commit('SET_TENANTNAME', data.tenant.tenantName)
            resolve(response)
          }else{
            resolve(response)
          }
         

        }).catch(error => {
          reject(error)

        })


        // this.post({},'services/app/common/GetCurrentUserInfo').then(rs=>{
        //     console.log(rs)
        // })


        // getInfo(state.token).then(response => {
        //   const data = response.result
        //   console.log(data)
        //   if (data.roleInfo ) { // 验证返回的roles是否是一个非空数组
        //     commit('SET_ROLES', data.roleInfo)
        //   } else {
        //     reject('getInfo: roles must be a non-null array !')
        //   }
        //   commit('SET_NAME', data.userInfo.userName)
        //   //commit('SET_AVATAR', data.avatar)
        //   resolve(response)
        // }).catch(error => {
        //   reject(error)
        // })
      })
    },

    // 登出
    LogOut({ commit, state }) {
      return new Promise((resolve, reject) => {
        logout(state.token).then(() => {
          commit('SET_TOKEN', '')
          commit('SET_ROLES', [])
          removeToken()
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 前端 登出
    FedLogOut({ commit }) {
      return new Promise(resolve => {
        localStorage.removeItem('baseuserinfo');
        commit('SET_TOKEN', '')
        removeToken()
        resolve('success')
      })
    }
  }
}

export default user
